<?php
namespace Home\Controller;
use Think\Controller;
class DressController extends BaseController {
    public function dress(){


      $dressModel=M('dress');//表明,conf中定义前缀
      //$userModel=M('users');
    /*  $username=session('username');
     */
      $dress= $dressModel->order('dressid asc')->select(); //desc降序 asc升序
      $this->dress=$dress;//name属性对应的值 $this->对应值=$goods;

      $this->display();
  

    } 


 public function dressadd(){
        $dressModel = M('dress'); // 实例化User模型
        $createresult=$dressModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
                $dressModel->userid = $_SESSION['user_id'];
                $username=$dressModel->username = $_SESSION['username'];
                $recipientsname=$dressModel->recipientsname = trim(I('post.recipientsname'));
                $detaildress=$dressModel->detaildress = trim(I('post.detaildress'));
                $telephone=$dressModel->tel = trim(I('post.tel'));
                $special=$dressModel->special = trim(I('post.special'));   
                $createtime=$dressModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
    
                 $newid=$dressModel->add(); // 写入用户数据到数据库
                     if (false !== $newid)
                             {
                            $value = "发件人： $username \r 收件人： $recipientsname \r 联系电话：$tel \r 地址： $detaildress \r 备注：$special \r 详细内容:http://".$_SERVER['SERVER_NAME'].U("dress/detail",array("dressid"=>$newid)); 
                            echo $value;
                            $qrpath=$this->qrcode($value);
                            $dressModel->qrpath = $qrpath;// 保存上传的照片根据需要自行组装 
                            $dressModel->where("dressid=$newid")->save();
                            $this->success('生成二维码成功！', '');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($usersModel->getError());  
              }

}

public function qrcode($value)
    {
       Vendor('phpqrcode.phpqrcode');
       //$value = 'http://www.cnblogs.com/txw1958/'; //二维码内容
      //$value = "名字：1 联系人：2 联系电话:3 邮箱:4 备注：6";  
       $errorCorrectionLevel = "3" ;//容错级别 
       $matrixPointSize = "7";//生成图片大小 
       //生成二维码图片
       $qrpath=date("YmdHis").rand(100,999).'.png';
       $qrpathinfo="Public/Uploads/qrpath/".$qrpath;
       $object = new \QRcode();
       $object->png($value, $qrpathinfo, $errorCorrectionLevel, $matrixPointSize, 2);
       $logo = "Public/Uploads/qrpath/logo.png";//准备好的logo图片  
       $QR = $qrpathinfo;//已经生成的原始二维码图
       //echo "$logo";
       //echo "$QR";
       if ($logo !== FALSE) {  
          $QR = imagecreatefromstring(file_get_contents($QR));  
          $logo = imagecreatefromstring(file_get_contents($logo));  
          $QR_width = imagesx($QR);//二维码图片宽度  
          $QR_height = imagesy($QR);//二维码图片高度  
          $logo_width = imagesx($logo);//logo图片宽度  
          $logo_height = imagesy($logo);//logo图片高度  
          $logo_qr_width = $QR_width / 5;  
          $scale = $logo_width/$logo_qr_width;  
          $logo_qr_height = $logo_height/$scale;  
          $from_width = ($QR_width - $logo_qr_width) / 2;  
          //重新组合图片并调整大小
          //echo "$QR_width";
  imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,  
  $logo_qr_height, $logo_width, $logo_height);  
            
            //框架生成二维码LOGO
            //$image = new \Think\Image();
            //$image->open($qrpathinfo)->water($logo,\Think\Image::IMAGE_WATER_CENTER,100)->save($qrpathinfo);
}  
//输出图片  
imagepng($QR,"$qrpathinfo");
return $qrpath;  
    }




    public function delqrcode(){
      //dump($_GET);
      $dressid=$_GET['dressid'];
      //echo $id;
      $dressModel = M("dress"); // 实例化User对象
      $dress=$dressModel->where("dressid=$dressid")->select();
      $result=$dressModel->where("dressid=$dressid")->delete(); // 删除id为5的用户数据
  
   if ($dress[0]['qrpath']!='nopic.png') {
      $picPath='Public/Uploads/qrpath/'.$dress[0]['qrpath'];
      unlink($picPath);
    }
    if($result){          
        $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
      } else {
         $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
      }

    }









}